package com.xin.springcloudconsumer.aspect;

import com.xin.springcloudconsumer.exception.LoginAspectException;
import com.xin.springcloudconsumer.utils.SessionValueUtils;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

@Aspect
@Component
@Slf4j
public class LoginAspect {
    /*切入点*/
    @Pointcut("execution(public * com.xin.springcloudconsumer.controller.EventController.*(..))")
    public void verify() {
    }
    @Before("verify()")
    public void doVerify() throws LoginAspectException {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();
        String openId = SessionValueUtils.getOpenId(request);
        String headImage=SessionValueUtils.getHeadImage(request);
        if (StringUtils.isEmpty(openId)||StringUtils.isEmpty(headImage)){
                log.info("还未登录!");
                throw new LoginAspectException("请先登录!");
        }
    }


}


